<?php

class DB_Functions {

    private $db;

    //put your code here
    // constructor
    function __construct() {
        include_once 'config/db_connect.php';
        // connecting to database
        $this->db = new DB_Connect();
        $this->db->connect();
    }

    // destructor
    function __destruct() {
        
    }

    /**
     * Storing new user
     * returns user details
     */
    public function storeUser($name, $email, $gcm_regid) {
        // insert user into database
        $result = mysql_query("INSERT INTO gcm_users(name, email, gcm_regid, created_at) VALUES('$name', '$email', '$gcm_regid', NOW())");
        // check for successful store
        if ($result) {
            // get user details
            $id = mysql_insert_id(); // last inserted id
            $result = mysql_query("SELECT * FROM gcm_users WHERE id = $id") or die(mysql_error());
            // return user details
            if (mysql_num_rows($result) > 0) {
                return mysql_fetch_array($result);
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    public function storePath($toko, $path, $warna) {
        // insert user into database
        $result = mysql_query("INSERT INTO goefence(nama_toko, path, warna) VALUES('$toko', '$path', '$warna')");
    }

    /**
     * Get user by email and password
     */
    public function getUserByEmail($email) {
        $result = mysql_query("SELECT * FROM gcm_users WHERE email = '$email' LIMIT 1");
        return $result;
    }

    public function cekPoly($point, $polygon) {
        if ($polygon[0] != $polygon[count($polygon) - 1]) {
            $polygon[count($polygon)] = $polygon[0];
        }
        $j = 0;
        $oddNodes = false;
        $x = $point[1];
        $y = $point[0];
        $n = count($polygon);
        for ($i = 0; $i < $n; $i++) {
            $j++;
            if ($j == $n) {
                $j = 0;
            }
            if ((($polygon[$i][0] < $y) && ($polygon[$j][0] >= $y)) || (($polygon[$j][0] < $y) && ($polygon[$i][0] >=
                    $y))) {
                if ($polygon[$i][1] + ($y - $polygon[$i][0]) / ($polygon[$j][0] - $polygon[$i][0]) * ($polygon[$j][1] -
                        $polygon[$i][1]) < $x) {
                    $oddNodes = !$oddNodes;
                }
            }
        }
        return $oddNodes;
    }

    public function convert($string) {
        $kurung = array("(", ")");
        $siku = array("[", "],");

        $new = str_replace($kurung, $siku, $string);

        return substr($new, 0, -1);
    }

    /**
     * Getting all users
     */
    public function getAllUsers() {
        $result = mysql_query("select * FROM gcm_users");
        return $result;
    }

    public function deleteIklan($id) {
        $result = mysql_query("DELETE FROM `iklan` WHERE `id`='$id'");
       // return $result;
    }

    public function getIklan($id) {
        $result = mysql_query("SELECT * FROM iklan WHERE geofence_id='$id' LIMIT 1");
        return $result;
    }

    public function getAllIklan() {
        $result = mysql_query("SELECT * FROM `iklan`");
        return $result;
    }

    public function updateIklan($id, $geofence_id, $title, $message, $url, $image_url) {
        $sql = mysql_query("UPDATE `iklan` SET `geofence_id` = '$geofence_id' ,  `title` =  '$title' ,  `message` =  '$message' ,  `url` =  '$url' ,  `image_url` =  '$image_url'   WHERE `id` = '$id' ");
        //return $result;
    }

    public function insertIklan($id, $title, $message, $url, $image_url) {
        $result = mysql_query("INSERT INTO iklan (`geofence_id`, `title`, `message`, `url`, `image_url`) VALUES ('$id','$title','$message','$url','$image_url')");
    }

    public function getAllGeo() {
        $result = mysql_query("select * FROM goefence");
        return $result;
    }

    /**
     * Check user is existed or not
     */
    public function isUserExisted($email) {
        $result = mysql_query("SELECT email from gcm_users WHERE email = '$email'");
        $no_of_rows = mysql_num_rows($result);
        if ($no_of_rows > 0) {
            // user existed
            return true;
        } else {
            // user not existed
            return false;
        }
    }

}

?>